using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
using System.Collections;

namespace Devfarm
{
	[System.ComponentModel.DescriptionAttribute("")]
    public sealed partial class HelloWorld
    {
/// <summary>
/// this is the summary
/// </summary>
/// <param name="MessageText">MessageText</param>
/// <returns></returns>
[Devfarm.Configuration.HelpAttribute(@"MessageText", "Enter the text you'd like to be displayed in the MessageBox.")]
[Devfarm.Configuration.UserDefaultValueAttribute("MessageText", @"Hello, World!")]
[Devfarm.Configuration.AliasAttribute("MessageText", "MessageText")]

        public static void Execute(System.Object MessageText)
        {
			Devfarm.BootStrap.AddResolver();
            Dictionary<string, object> args = new Dictionary<string, object>();

        if(MessageText != null)
            args.Add("MessageText", MessageText);
        else
            args.Add("MessageText", "Hello, World!");

             InnerExecute(args);
        }
        
        private static void InnerExecute(Dictionary<string, object> parameters)
        {
            using(WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
				Dictionary<string, object> workflowParameters = null;

                AutoResetEvent waitHandle = new AutoResetEvent(false);
				workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) 
				{
					workflowParameters = e.OutputParameters;
					waitHandle.Set(); 
				};
                workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };

                Dictionary<string, object> defaultParameters = Devfarm.BootStrap.GetWorkflowDefaultParms(typeof(Devfarm.HelloWorld));
                foreach (string key in parameters.Keys)
                {
					// overwrite the defaults with those passed
					defaultParameters[key] = parameters[key];
                }

                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(Devfarm.HelloWorld), defaultParameters);
                instance.Start();

                waitHandle.WaitOne();
                
return;                
            }
        }
    }
}
